package com.platform.common.util.encrypt;

import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * BASE64 编码工具类
 * 
 * @author Administrator
 * 
 */
public class Base64Util {
	// 将 s 进行 BASE64 编码
	public static String getBASE64(String s) {
		if (s == null)
			return null;
		try {
			String str = (new BASE64Encoder()).encode(s.getBytes("UTF-8"));
			str = str.replaceAll("\\+", "-");
			str = str.replaceAll("\\/", "*");
			return str;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	// 将 BASE64 编码的字符串 s 进行解码
	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			s = s.replaceAll("-", "+");
			s = s.replaceAll("\\*", "/");
			byte[] b = decoder.decodeBuffer(s);
			return new String(b, "UTF-8");
		} catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) {
		String str = "省市县发文流程";
		String base64 = getBASE64(str);
		System.out.println(base64);
	}

}
